package com.atguigu.gmall.payment.service.impl.impl;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;


    @Override
    public void savePaymentInfo(OrderInfo orderInfo,String paymentType) {

        // 支付方式从页面获取  PaymentType.ALIPAY
        /*
            1. 声明一个paymentInfo 对象，这个对象必须有数据
            2. 调用mapper插入数据
            3. 细节：订单id，支付方式，这两个条件组成的数据只能有一条
         */
        // 通过sql语句查询
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderInfo.getId());
        paymentInfoQueryWrapper.eq("payment_type",paymentType);
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);
        // 判断是否有数据
        if(paymentInfoQuery != null){
            return;
        }

        PaymentInfo paymentInfo = new PaymentInfo();
        // 第三方业务编号
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
//        paymentInfo.setTradeNo();
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setCreateTime(new Date());

        paymentInfoMapper.insert(paymentInfo);

    }

    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String name) {
        // select * from payment_info where out_trade_no = ? and payment_type = ?
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("out_trade_no",outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type",name);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);
        return paymentInfo;
    }

    @Override
    public void paySuccess(String outTradeNo, String name, Map<String, String> paramMap) {
        // 通过outTradeNo ,name 可以查询到paymentInfo对象
        PaymentInfo paymentInfoQuery = getPaymentInfo(outTradeNo, name);
        // 判断
        if("PAID".equals(paymentInfoQuery.getPaymentStatus()) || "CLOSED".equals(paymentInfoQuery.getPaymentStatus())){
            return;
        }

        // 调用mapper
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setTradeNo(paramMap.get("trade_no"));
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackContent(paramMap.toString());


//        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper();
//        paymentInfoQueryWrapper.eq("out_trade_no",outTradeNo);
//        paymentInfoQueryWrapper.eq("payment_type",name);
//        // 第一个参数，这是更新的内容，第二个参数表示更新的条件
//        paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);
        this.updatePaymentInfo(outTradeNo,name,paymentInfo);

        // 发送消息给订单
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,paymentInfoQuery.getOrderId());
    }

    // 更新交易状态记录
    @Override
    public void updatePaymentInfo(String outTradeNo, String name, PaymentInfo paymentInfo) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper();
        paymentInfoQueryWrapper.eq("out_trade_no",outTradeNo);
        paymentInfoQueryWrapper.eq("payment_type",name);
        // 第一个参数，这是更新的内容，第二个参数表示更新的条件
        paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);
    }

    @Override
    public void closePayment(Long orderId) {
        // 关闭方法
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id",orderId);
        // 做一个判断
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(paymentInfoQueryWrapper);
        if(paymentInfoQuery == null ){
            return;
        }
        // 第一个参数修改的内容，第二个参数条件
        paymentInfoMapper.update(paymentInfo,paymentInfoQueryWrapper);
    }
}
